package cn.itcast.web.controller.cargo;

import cn.hutool.core.bean.BeanUtil;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.*;
import cn.itcast.util.DownloadUtils;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/cargo/export", name = "pdf报表生成")
public class PdfController extends BaseController {
    @Reference
    private ExportService exportService;

    @Reference
    private ExportProductService exportProductService;

    @Reference
    private PackingService packingService;
    @Reference
    private ShippingService shippingService;

    @Reference
    private InvoiceService invoiceService;
    @Reference
    private FinanceService financeService;

    @RequestMapping("/exportPdf")
    public void exportPdf(String id) throws Exception {
        // 1.加载模板
        String realPath = request.getSession().getServletContext().getRealPath("/jasper/export.jasper");
        // 2.填充数据
    /*
            参数一：模板路径
            参数二：map集合类型数据
            参数三：list集合类型数据（JRDataSource）
         */

        // 2.1查询报运单对象转为map集合
        Export export = exportService.findById(id);
        // hutool工具包，可以将对象转为map集合，key=对象属性名，value=对象属性值
        Map<String, Object> map = BeanUtil.beanToMap(export);

        // 2.2查询报运单商品列表转为JRDataSource
        ExportProductExample exportProductExample = new ExportProductExample();
        exportProductExample.createCriteria().andExportIdEqualTo(id);
        List<ExportProduct> list = exportProductService.findAll(exportProductExample);
        JRBeanCollectionDataSource jrDataSource = new JRBeanCollectionDataSource(list);

        JasperPrint jasperPrint = JasperFillManager.fillReport(realPath, map, jrDataSource);

        //  3.生成PDF输出到客户端
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
    }
    @RequestMapping("/packingPdf")
    public void packingPdf(String id) throws Exception {
        // 1.加载模板
        String realPath = request.getSession().getServletContext().getRealPath("/jasper/packing.jasper");

        // 2.1查询报运单对象转为map集合
        Packing packing = packingService.selectByPrimaryKey(id);
        // hutool工具包，可以将对象转为map集合，key=对象属性名，value=对象属性值
        Map<String, Object> map = BeanUtil.beanToMap(packing);



        JasperPrint jasperPrint = JasperFillManager.fillReport(realPath, map, new JREmptyDataSource());

        //  3.生成PDF输出到客户端
       // JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

        // 4.pdf文件下载
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // 创建字节数组输出流
        JasperExportManager.exportReportToPdfStream(jasperPrint, byteArrayOutputStream); // 将pdf输出到字节数组输出流中
        DownloadUtils.download(byteArrayOutputStream, response, "装箱单.pdf");// 完成文件下载
    }
    @RequestMapping("/shippingPdf")
    public void shippingPdf(String id) throws Exception {
        // 1.加载模板
        String realPath = request.getSession().getServletContext().getRealPath("/jasper/shipping.jasper");

        // 2.1查询报运单对象转为map集合
        Shipping shipping = shippingService.selectByPrimaryKey(id);
        // hutool工具包，可以将对象转为map集合，key=对象属性名，value=对象属性值
        Map<String, Object> map = BeanUtil.beanToMap(shipping);



        JasperPrint jasperPrint = JasperFillManager.fillReport(realPath, map, new JREmptyDataSource());

        //  3.生成PDF输出到客户端
        // JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

        // 4.pdf文件下载
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // 创建字节数组输出流
        JasperExportManager.exportReportToPdfStream(jasperPrint, byteArrayOutputStream); // 将pdf输出到字节数组输出流中
        DownloadUtils.download(byteArrayOutputStream, response, "委托单.pdf");// 完成文件下载
    }

    @RequestMapping("/invoicePdf")
    public void invoicePdf(String id) throws Exception {
        // 1.加载模板
        String realPath = request.getSession().getServletContext().getRealPath("/jasper/invoice.jasper");

        // 2.1查询报运单对象转为map集合
        Invoice invoice = invoiceService.selectByPrimaryKey(id);
        // hutool工具包，可以将对象转为map集合，key=对象属性名，value=对象属性值
        Map<String, Object> map = BeanUtil.beanToMap(invoice);



        JasperPrint jasperPrint = JasperFillManager.fillReport(realPath, map, new JREmptyDataSource());

        //  3.生成PDF输出到客户端
        // JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

        // 4.pdf文件下载
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // 创建字节数组输出流
        JasperExportManager.exportReportToPdfStream(jasperPrint, byteArrayOutputStream); // 将pdf输出到字节数组输出流中
        DownloadUtils.download(byteArrayOutputStream, response, "发票单.pdf");// 完成文件下载
    }

    @RequestMapping("/financePdf")
    public void financePdf(String id) throws Exception {
        // 1.加载模板
        String realPath = request.getSession().getServletContext().getRealPath("/jasper/finance.jasper");

        // 2.1查询报运单对象转为map集合
        Finance finance = financeService.selectByPrimaryKey(id);
        // hutool工具包，可以将对象转为map集合，key=对象属性名，value=对象属性值
        Map<String, Object> map = BeanUtil.beanToMap(finance);



        JasperPrint jasperPrint = JasperFillManager.fillReport(realPath, map, new JREmptyDataSource());

        //  3.生成PDF输出到客户端
        // JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

        // 4.pdf文件下载
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // 创建字节数组输出流
        JasperExportManager.exportReportToPdfStream(jasperPrint, byteArrayOutputStream); // 将pdf输出到字节数组输出流中
        DownloadUtils.download(byteArrayOutputStream, response, "财务单.pdf");// 完成文件下载
    }
}
